<?php
class CI_Html extends MX_Controller {
	var $base = null;
	function _setbase($db){
		$this->base = $db;
	}
	/*validate 
		required: "กรุณากรอกข้อมูล.",
		remote: "Please fix this field.",
		email: "Please enter a valid email address.",
		url: "Please enter a valid URL.",
		date: "Please enter a valid date.",
		dateISO: "Please enter a valid date (ISO).",
		dateDE: "Bitte geben Sie ein gültiges Datum ein.",
		number: "Please enter a valid number.",
		numberDE: "Bitte geben Sie eine Nummer ein.",
		digits: "Please enter only digits",
		creditcard: "Please enter a valid credit card number.",
		equalTo: "Please enter the same value again.",
		accept: "Please enter a value with a valid extension.", (accept=\"gif|jpg\")
		maxlength: $.format("Please enter no more than {0} characters."),
		minlength: $.format("Please enter at least {0} characters."),
		rangelength: $.format("Please enter a value between {0} and {1} characters long."),
		range: $.format("Please enter a value between {0} and {1}."),
		max: $.format("Please enter a value less than or equal to {0}."),
		min: $.format("Please enter a value greater than or equal to {0}.")
	*/
	//รูปแบบการเรียใช้งาน $this->html->get_text(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","size"=>"","maxlength"=>""));
	function get_text($obj=array()){
		$obj['value'] = trim(@$obj['value']);
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$obj['size'] = (!@$obj['size']) ? "20" : @$obj['size'];
		return '<input type = "text" id="'.@$obj['id'].'" name = "'.@$obj['name'].'" value = "'.@$obj['value'].'" maxlength="'.@$obj['maxlength'].'" class = "txtinput '.@$obj['class'].'" size = "'.@$obj['size'].'" '.@$obj['event'].'>';
	}
	//รูปแบบการเรียใช้งาน $this->html->get_password(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","size"=>""));
	function get_password($obj=array()){
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$obj['size'] = (!@$obj['size']) ? "20" : @$obj['size'];
		return '<input type = "password" id="'.@$obj['id'].'" name = "'.@$obj['name'].'" value = "'.@$obj['value'].'" maxlength="25" class = "txtinput '.@$obj['class'].'" size = "'.@$obj['size'].'" '.@$obj['event'].'>';
	}
	//รูปแบบการเรียใช้งาน $this->html->get_upload(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","size"=>""));
	function get_upload($obj=array()){
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$obj['size'] = (!@$obj['size']) ? "25" : @$obj['size'];
		return '<input type = "file" id="'.@$obj['id'].'" name = "'.@$obj['name'].'" value = "'.@$obj['value'].'" class = "system_input_upload '.@$obj['class'].'" size = "'.@$obj['size'].'" '.@$obj['event'].'>';
	}
	//รูปแบบการเรียใช้งาน $this->html->get_textarea(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","width"=>"","height"=>""));
	function get_textarea($obj=array()){
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$obj['width'] = (!@$obj['width']) ? "30" : @$obj['width'];
		$obj['height'] = (!@$obj['height']) ? "4" : @$obj['height'];
		return '<textarea id="'.@$obj['id'].'" name = "'.@$obj['name'].'" cols = "'.@$obj['width'].'" rows = "'.@$obj['height'].'" class="txtarea '.@$obj['class'].'" '.@$obj['event'].'>'.@$obj['value'].'</textarea>';
	}
	//รูปแบบการเรียใช้งาน $this->html->get_radio(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","title"=>"","checkobj"=>""));
	function get_radio($obj=array()){
		@$obj['checkobj'] = (string)@$obj['checkobj'];
		@$obj['value'] = (string)@$obj['value'];
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		if(@$obj['value']==@$obj['checkobj']){
			$checked="checked";
		}
		return '<input type = "radio" id="'.@$obj['id'].'" name = "'.@$obj['name'].'" value = "'.@$obj['value'].'" class = "'.@$obj['class'].'" '.@$checked.' '.@$obj['event'].'>&nbsp;'.@$obj['title'];
	}
	//รูปแบบการเรียใช้งาน $this->html->get_check(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","title"=>"","checkobj"=>""));
	function get_check($obj=array()){
		@$obj['checkobj'] = (string)@$obj['checkobj'];
		@$obj['value'] = (string)@$obj['value'];
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$obj['checkobj'] = (@$obj['value']==@$obj['checkobj']) ? "checked" : "";
		return '<input type = "checkbox" id="'.@$obj['id'].'" name = "'.@$obj['name'].'" value = "'.@$obj['value'].'" class = "'.@$obj['class'].'" '.@$obj['checkobj'].' '.@$obj['event'].'>&nbsp;'.@$obj['title'];
	}
	//รูปแบบการเรียใช้งาน $this->html->get_select(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","title"=>"","selectobj"=>array()));
	function get_select($obj=array()){
		@$obj['value'] = (string)@$obj['value'];
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$selectlist = '<select name = "'.@$obj['name'].'" id = "'.@$obj['id'].'" class = "txtselect '.@$obj['class'].'" '.@$obj['event'].'>';
		$selectlist.= (@$obj['title']) ? '<option value = "" selected>'.@$obj['title'].'</option>' : '';
		if(count(@$obj['selectobj'])>0){
			foreach(@$obj['selectobj'] as $value=>$text){
				$selected = "";
				@$value = (string)@$value;
				if($value==@$obj['value']){
					$selected = "selected";
				}
				$selectlist .= '<option value="'.@$value.'" '.$selected.'>'.@$text.'</option>';
			}
		}
		$selectlist.= "</select>";
		return $selectlist;
	}
	//รูปแบบการเรียใช้งาน $this->html->get_select_database(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","title"=>"","selectobj"=>array(),"field_value"=>"","field_text"=>""));
	function get_select_database($obj=array()){
		@$obj['value'] = (string)@$obj['value'];
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$selectlist = '<select name = "'.@$obj['name'].'" id = "'.@$obj['id'].'" class = "txtselect '.@$obj['class'].'" '.@$obj['event'].'>';
		$selectlist.= (@$obj['title']) ? '<option value = "" selected>'.@$obj['title'].'</option>' : '';
		if(count(@$obj['selectobj'])>0){
			foreach(@$obj['selectobj'] as $values){
				@$values->$obj['field_value'] = (string)@$values->$obj['field_value'];
				$selected = "";
				if($values->$obj['field_value']==@$obj['value'] && @$obj['value']!=""){
					$selected = "selected";
				}
				$selectlist .= '<option value="'.@$values->$obj['field_value'].'" '.$selected.'>'.@$values->$obj['field_text'].'</option>';
			}
		}
		$selectlist.= "</select>";
		return $selectlist;
	}
	//รูปแบบการเรียใช้งาน $this->html->get_selectmulti_database(array("id"=>"","name"=>"","value"=>array(),"class"=>"","event"=>"","title"=>"","selectobj"=>array(),"field_value"=>"","field_text"=>""));
	function get_selectmulti_database($obj=array()){
		@$obj['value'] = (string)@$obj['value'];
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$selectlist = '<select name = "'.@$obj['name'].'[]" id = "'.@$obj['id'].'" class = "multiple  '.@$obj['class'].'" '.@$obj['event'].' multiple="multiple">';

		if($obj['value']!="") :
			$selectlist.= (@$obj['title']) ? '<option value = "">'.@$obj['title'].'</option>' : '';
		else :
			$selectlist.= (@$obj['title']) ? '<option value = "" selected>'.@$obj['title'].'</option>' : '';
		endif;

		if(count(@$obj['selectobj'])>0){
			foreach(@$obj['selectobj'] as $values){
				@$values->$obj['field_value'] = (string)@$values->$obj['field_value'];
				$selected = "";
				$array = (@$obj['value']!="" && count(@$obj['value'])>0) ? explode(",",@$obj['value']) : array();
				if(in_array($values->$obj['field_value'],@$array)){
					$selected = "selected";
				}
				$selectlist .= '<option value="'.@$values->$obj['field_value'].'" '.$selected.'>'.@$values->$obj['field_text'].'</option>';
			}
		}
		$selectlist.= "</select>";
		return $selectlist;
	}
	//รูปแบบการเรียใช้งาน $this->html->get_button(array("id"=>"","name"=>"","value"=>"","type"=>"","event"=>""));
	function get_button($obj=array()){
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		return '<input type = "'.@$obj['type'].'" id="'.@$obj['id'].'" name = "'.@$obj['name'].'" value = "'.@$obj['value'].'" class="button '.@$obj['class'].'" '.@$obj['event'].' style="cursor:pointer">';
	}
	//รูปแบบการเรียใช้งาน $this->html->get_editor(array("name"=>"","value"=>"","width"=>"100%","height"=>"100"));
	function get_editor($obj=array()){
		$html  = '<script type="text/javascript" src="'.URLPATH_JS.'editor/tiny_mce/jquery.tinymce.js"></script>';
		$html .= '<script type="text/javascript" src="'.URLPATH_JS.'editor/tiny_mce/config.js"></script>';
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$obj['width'] = (!@$obj['width']) ? "30" : @$obj['width'];
		$obj['height'] = (!@$obj['height']) ? "4" : @$obj['height'];
		$html .= '<textarea id="'.@$obj['id'].'" name = "'.@$obj['name'].'" cols = "'.@$obj['width'].'" rows = "'.@$obj['height'].'" class="txtarea editor'.@$obj['class'].'" '.@$obj['event'].'>'.@$obj['value'].'</textarea>';
		return $html;
	}
	//รูปแบบการเรียใช้งาน $this->html->get_time_hour(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","title"=>""));
	function get_time_hour($obj=array()){
		@$obj['value'] = (string)@$obj['value'];
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$selectlist = '<select name = "'.@$obj['name'].'" id = "'.@$obj['id'].'" class = "txtselect '.@$obj['class'].'" '.@$obj['event'].'>';
		$selectlist.= (@$obj['title']) ? '<option value = "" selected>'.@$obj['title'].'</option>' : '';
			for($i=0;$i<24;$i++){
				$hour = sprintf("%02d",$i);
				if(trim(@$obj["value"])=="null"){
					$value = "00";
				}else if(trim(@$obj["value"])!=""){
					$value = @$obj["value"];
				}else{
					$value = date("H");
				}
				if($hour==$value && $value!=""){
					$selected = "selected";
				} else {
					$selected = "";
				}
				$selectlist .= '<option value="'.@$hour.'" '.$selected.'>'.@$hour.'</option>';
		}
		$selectlist.= "</select>";
		return $selectlist;
	}
	//รูปแบบการเรียใช้งาน $this->html->get_time_minute(array("id"=>"","name"=>"","value"=>"","class"=>"","event"=>"","title"=>""));
	function get_time_minute($obj=array()){
		@$obj['value'] = (string)@$obj['value'];
		$obj['id'] = (!@$obj['id']) ? @$obj['name'] : @$obj['id'];
		$selectlist = '<select name = "'.@$obj['name'].'" id = "'.@$obj['id'].'" class = "txtselect '.@$obj['class'].'" '.@$obj['event'].'>';
		$selectlist.= (@$obj['title']) ? '<option value = "" selected>'.@$obj['title'].'</option>' : '';
			for($i=0;$i<60;$i++){
				$minute = sprintf("%02d",$i);
				if(trim(@$obj["value"])=="null"){
					$value = "00";
				}else if(trim(@$obj["value"])!=""){
					$value = @$obj["value"];
				}else{
					$value = date("i");
				}
				if($minute==$value && $value!=""){
					$selected = "selected";
				} else {
					$selected = "";
				}
				$selectlist .= '<option value="'.@$minute.'" '.$selected.'>'.@$minute.'</option>';
		}
		$selectlist.= "</select>";
		return $selectlist;
	}
	//รูปแบบการเรียใช้งาน $this->html->get_carendar_obj1(array("name"=>"","value"=>"","class"=>""));
	function get_carendar_obj1($obj=array()){
		/*if(trim(@$obj["value"])==""){
			$value = date('d-m-').(date('Y')+543);
		}else{
			$value = @$obj["value"];
		}*/
		$year = intval($this->lang->line("Add_Year"),10);//thai = 543
		if(trim(@$obj["value"])=="null"){
			$value = "";
		}else if(trim(@$obj["value"])!=""){
			$value = @$obj["value"];
		}else{
			$value = date('d-m-').(date('Y')+$year);
		}
		$this->Inputdate 				= load_class('Creinputdate');
		$this->Inputdate->URLPATH		= URLPATH;
		$this->Inputdate->inputmonth	= 1;
		$this->Inputdate->inputid		= @$obj["name"];
		$this->Inputdate->inputclass	= 'txtinput calendar '.@$obj["class"];
		$this->Inputdate->inputvalue	= $value;
		return "<span id=\"imgdate\">".$this->Inputdate->_geninputDate()."</span>";
	}
	//รูปแบบการเรียใช้งาน $this->html->get_carendar_obj2(array("namestart"=>"","nameend"=>"","valuestart"=>"","valueend"=>"","class"=>""));
	function get_carendar_obj2($obj=array()){
		/*if(trim(@$obj["valuestart"])==""){
			$valuestart = date('d-m-').(date('Y')+543);
		}else{
			$valuestart = @$obj["valuestart"];
		}
		if(trim(@$obj["valueend"])==""){
			$valueend = date('d-m-').(date('Y')+543);
		}else{
			$valueend = @$obj["valueend"];
		}*/
		$year = intval($this->lang->line("Add_Year"),10);//thai = 543
		if(trim(@$obj["valuestart"])=="null"){
			$valuestart = "";
		}else if(trim(@$obj["valuestart"])!=""){
			$valuestart = @$obj["valuestart"];
		}else{
			$valuestart = date('d-m-').(date('Y')+$year);
		}
		if(trim(@$obj["valueend"])=="null"){
			$valueend = "";
		}else if(trim(@$obj["valueend"])!=""){
			$valueend = @$obj["valueend"];
		}else{
			$valueend = date('d-m-').(date('Y')+$year);
		}
		$this->Inputdate 					= load_class('Creinputdate');
		$this->Inputdate->URLPATH			= URLPATH;
		$this->Inputdate->inputmonth		= 2;
		$this->Inputdate->inputclass		= 'system_input_left '.@$obj["class"];
		$this->Inputdate->inputidStart		= @$obj["namestart"];
		$this->Inputdate->inputidFinish		= @$obj["nameend"];
	 	$this->Inputdate->inputLengLabel	= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="text3">ถึงวันที่</span>'; 
		$this->Inputdate->inputvalueStart	= $valuestart;
		$this->Inputdate->inputvalueFinish	= $valueend;
		return "<span id=\"imgdate\">".$this->Inputdate->getInputDateLenght()."</span>";
	}
	function get_auto_complete ($obj=array()) {
		
		$data = '';
		
		# Set default
		$obj['id']     = (isset($obj['id']))     ? $obj['id']     : 'autocomplete';
		$obj['name']   = (isset($obj['name']))   ? $obj['name']   : 'tumbon';
		$obj['class']  = (isset($obj['class']))  ? $obj['class']  : 'required';
		
		if(isset($obj['db_name']) && isset($obj['db_field'])) {
		
			$this->base->select($obj['db_field']);
			
			if (isset($obj['db_where'])) {
				$this->base->where($obj['db_where']);
			}
			
			$query = $this->base->get($obj['db_name']);
			$rows  = $query->result();
		
			foreach ($rows as $values) {
			
				$data .= $values->$obj['db_field']."|";
			}			
		}
		
		$html  = '  <script type="text/javascript" src="'.URLPATH.'system/plugins/jquery-autocomplete/jquery.autocomplete.js"></script>
					<link rel="stylesheet" type="text/css" href="'.URLPATH.'system/plugins/jquery-autocomplete/jquery.autocomplete.css" />
					<script>
						  $(document).ready(function(){
							var data = "'.$data.'".split("|");
							$("#'.$obj['id'].'").autocomplete(data, {
								matchContains: true,
								minChars: 0
							});
						  });
					</script>
					';
		$html .= $this->get_text($obj);
		
		return $html;
	}
}
?>